Skip to content

feat: prefix dictionaries (refPrefix) for delta compression#27

Merged
dfa1 merged 2 commits into
mainfrom
feat/ref-prefix
Jun 27, 2026
Merged

feat: prefix dictionaries (refPrefix) for delta compression#27
dfa1 merged 2 commits into
mainfrom
feat/ref-prefix

Conversation

@dfa1

@dfa1 dfa1 commented Jun 27, 2026

Copy link
Copy Markdown
Owner

What

Binds ZSTD_CCtx_refPrefix / ZSTD_DCtx_refPrefix as
ZstdCompressCtx.refPrefix(MemorySegment) / ZstdDecompressCtx.refPrefix(MemorySegment):
a single-use, by-reference raw-content prefix applied to the next frame only —
the building block for delta compression (compress a new version against a
similar previous one, storing little more than the difference).

Unlike loadDictionary, a prefix is not digested, not copied, and writes no
dictionary ID; the decompressor must reference the same prefix to decode.

Why segment-only (no byte[] overload)

refPrefix references the prefix by pointer — it must outlive the next
compression. A byte[] overload would have to copy, which both defeats the
zero-copy point and forces a context-held arena to keep the copy alive until
consumed (extra lifecycle state in reset/close). Heap callers that want a
copy already have the right tool: loadDictionary. So the API is segment-only,
caller-owned lifetime — matching zstd's semantics exactly.

Tests

RefPrefixTest (4 cases): round-trip with matching prefix, null clears, heap
segment rejected, and the key one — prefix is applied and required to decode:
16 KiB of random data identical to the prefix compresses to tens of bytes at
level 19 only with the prefix, and the frame cannot be decoded without it. A
round-trip-only test would pass even if refPrefix were a no-op; this one fails
if the prefix isn't actually wired through (it caught a real by-reference
lifetime bug during development).

Full module suite: 200 tests green, checkstyle clean.

Notes

  • docs/supported.md updated (Advanced parameters 12 → 14/38; zstd-jni does not
    expose refPrefix).
  • CHANGELOG.md Unreleased entry added.

🤖 Generated with Claude Code

dfa1 and others added 2 commits June 27, 2026 16:52
Bind ZSTD_CCtx_refPrefix / ZSTD_DCtx_refPrefix as
ZstdCompressCtx.refPrefix(MemorySegment) / ZstdDecompressCtx.refPrefix(...): a
single-use, by-reference raw-content dictionary for the next frame only — the
building block for delta compression (compress a new version against a similar
previous one). No digest, no copy, no dictionary ID written; the decompressor
must reference the same prefix to decode.

Segment-only by design. refPrefix references the prefix by pointer (unlike
loadDictionary, which copies internally), so the prefix must outlive the next
compression — a contract only a caller-owned native segment can honour. A
byte[] overload would have to copy, defeating the zero-copy point and forcing a
context-held arena to keep the copy alive; heap callers should use the copying
loadDictionary instead.

RefPrefixTest proves the prefix is actually applied (16 KiB of random data,
identical to the prefix, compresses to tens of bytes at level 19 only with the
prefix) and is required to decode — a round-trip-only test would pass even if
refPrefix were a no-op.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- prove refPrefix is single-use: prefix set once, two compressions, only the
  first frame shrinks (random data at level 19); second frame decodes with a
  plain decoder, confirming it does not stick across frames.
- assert ZstdDecompressCtx.refPrefix also rejects a heap segment (was only
  covered on the compress side).
- import java.util.Arrays instead of the fully-qualified inline reference.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dfa1 dfa1 merged commit 04602ae into main Jun 27, 2026
1 check passed
@dfa1 dfa1 deleted the feat/ref-prefix branch June 27, 2026 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant